/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.monitor.profile.init;

import com.hyts.monitor.profile.context.ProfilerContext;
import com.hyts.monitor.profile.loader.ProfilerLoader;
import com.hyts.monitor.profile.model.Profiler;

import java.text.DateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.ServiceLoader;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.monitor.profile.init
 * @author:LiBo/Alex
 * @create-date:2019-08-05 13:47
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class ProfilerInitLoader {

    /**
     * 初始化相关服务应用数据-
     */
    public static void init(){
        //serviceLoader服务加载操作
        Profiler profiler = null;
        List<ProfilerLoader> profilerLoaders = new LinkedList<>();
        ServiceLoader.load(ProfilerLoader.class).forEach(param->profilerLoaders.add(param));
        //进行排序操作
        profiler = profilerLoaders.parallelStream().sorted((p1,p2)->p2.order() - p1.order()).
                map(param->param.loadProfilerConfig()).findFirst().get();
        //判断是否超过结束时间
        String planStartTime = profiler.getScope().getStartTime();
        String planEndTime = profiler.getScope().getEndTime();
        String realTime = DateFormat.getTimeInstance().format(new Date());
        //是否大于结束时间
        boolean isMoreEndTime = realTime.compareTo(planEndTime) > 0;
        profiler.getAttribute().getCurrentMoreThanEndTime().set(isMoreEndTime);
        //操作是否小于开始时间
        boolean isLessStartTime = realTime.compareTo(planStartTime) < 0;
        //working是否出去操作状态操作
        profiler.getAttribute().getWorking().set(!isMoreEndTime && !isLessStartTime);
        ProfilerContext.setProfiler(profiler);
    }

    /**
     * 启动所有handler处理器线程工作
     */
    public static void start(){


    }

}
